/*
 * vertex.java
 *
 * Created on March 11, 2011, 2:56 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ifs.RnD.Graph;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import ifs.RnD.svnRead.DisplayRepositoryTree;
import ifs.RnD.svnRead.Node;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.tmatesoft.svn.core.SVNException;

/**
 *
 * @author CHJSLK
 */
public class BreadcrumVertex extends JLabel {

    boolean Final;
    private String value, repoUrl;
    DisplayRepositoryTree disRepoTree;
    RepositoryGraph canvas;
    Iterator iterator1;
    Iterator iterator2;

    /** Creates a new instance of BreadcrumVertex
     * @param text
     * @param canvs
     * @param url
     * @param dr
     */
    public BreadcrumVertex(String text, String url, final RepositoryGraph canvs, DisplayRepositoryTree dr) {
        if (disRepoTree == null) {
            disRepoTree = dr;
            canvas = canvs;
        }
        String revisionUrl = null;
        String nType = null;

        value = text;
        repoUrl = url;

        setFont(new Font("tahoma", 0, 12));

        setForeground(Color.white);
        setText(text);
        setHorizontalAlignment(0);
        setSize(100, 50);

        setIcon(new javax.swing.ImageIcon(getClass().getResource("/ifs/RnD/Resources/breadF2.JPG")));

        setHorizontalTextPosition(BreadcrumVertex.HORIZONTAL);

        addMouseListener(new java.awt.event.MouseAdapter() {

            /**
             * handles mouse click events.
             * @param evt
             * @exception Any exception
             * @return
             */
            public void mouseClicked(MouseEvent evt) {
                try {
                    mouseSingleClicked(evt);
                } catch (SVNException ex) {
                    Logger.getLogger(BreadcrumVertex.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            /**
             * changes label appearance and mouse pointer when mouse enter to a label.
             * @param
             * @exception Any exception
             * @return
             */
            public void mouseEntered(MouseEvent e) {
                Cursor cursor = Cursor.getDefaultCursor();
                //change cursor appearance to HAND_CURSOR when the mouse pointed on images
                cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
                setCursor(cursor);
            }
            /**
             * reset label appearance and mouse pointer to the default when mouse exit from a label.
             * @param
             * @exception Any exception
             * @return
             */
        });

        addMouseListener(new NodeMenuListener(revisionUrl, nType, disRepoTree));
    }

    /**
     * draw a oval on the label
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (Final) {
            g.drawRect(5, 5, 40, 30);
        }
    }

    /**
     * sets the label nodeUrl
     * @param text
     */
    public void setLableValue(String text) {
        value = text;
        setText(text);
    }

    /**
     * identifies the selected labe and gets its nodeUrl
     * @param evt
     * @return
     */
    public String getSelectedNode(MouseEvent evt) {
        BreadcrumVertex ver = (BreadcrumVertex) evt.getSource();
        return ver.value;
    }

    /**
     * handles events when double click on a label.
     * @param evt
     * @throws SVNException
     */
    public void mouseSingleClicked(MouseEvent evt) throws SVNException {
        List list = (List) disRepoTree.directTree.toList();
        iterator2 = list.iterator();
        while (iterator2.hasNext()) {
            Node node = (Node) iterator2.next();
            String nodeUrl = node.getSvnURL();
            if (nodeUrl.equals(repoUrl)) {

                canvas.drawGraph(node);
                canvas.setVisible(true);
            }
        }
    }
}
